package cate.game.fight.test;

import cate.common.table.d.GDCamp;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class TeamFightTestStat {


	private Map<Byte,CampInfo> camps = new HashMap<>();

	public void record(byte camp1,byte camp2,boolean win){
		camps.computeIfAbsent(camp1, k->new CampInfo(camp1)).record(win,camp2);
		camps.computeIfAbsent(camp2, k->new CampInfo(camp2)).record(!win,camp1);
	}

	public void printInfo(){
		camps.values().forEach(CampInfo::stat);

		int rank = 0;
		for (CampInfo campInfo : camps.values().stream().sorted(Comparator.comparing(c -> -c.winRate)).collect(Collectors.toList())) {
			rank++;
			StringBuilder sb = new StringBuilder();
			sb.append("排名:").append(rank);
			sb.append("【").append(GDCamp.Type.description(campInfo.camp)).append("系】");
			sb.append(" 胜率:").append(String.format("%.2f", campInfo.winRate)).append("%");
			for (EnemyInfo enemyInfo : campInfo.enemys.values().stream().sorted(Comparator.comparing(e -> -e.winRate)).collect(Collectors.toList())) {
				sb.append("对阵【").append(GDCamp.Type.description(enemyInfo.camp)).append("系】胜率:").append(String.format("%.2f", enemyInfo.winRate)).append("%");
			}
			System.out.println(sb);

		}


	}

	public static class CampInfo{

		public byte camp;

		public int total;

		public int winTime;

		public double winRate;

		public Map<Byte,EnemyInfo> enemys = new HashMap<>();

		public CampInfo(byte camp) {
			this.camp = camp;
		}

		public void record(boolean win,byte enemyCamp){
			total++;
			if(win){
				winTime++;
			}
			enemys.computeIfAbsent(enemyCamp, k->new EnemyInfo(enemyCamp)).record(win);
		}

		public void stat(){
			winRate = winTime*100d/total;
			enemys.values().forEach(EnemyInfo::stat);
		}
	}


	public static class EnemyInfo{
		public byte camp;

		public int total;

		public int winTime;

		public double winRate;

		public EnemyInfo(byte camp) {
			this.camp = camp;
		}

		public void record(boolean win) {
			total++;
			if (win) {
				winTime++;
			}
		}

		public void stat(){
			winRate = winTime*100d/total;
		}
	}


//	public static void main(String[] args) {
//
//		System.out.println(calCombine(50, 6));
//	}
//
//	private static long calCombine(int total,int num){
//		return calJC(total, total-num)/calJC(num, 1);
//	}
//
//	private static long calJC(int start, int end) {
//		long total = 1;
//		for (int i = start; i > end; i--) {
//			total *= i;
//		}
//		return total;
//	}


}
